#include "vertexarray.h"

VertexArray::VertexArray()
: vertices(NULL), verticesSize(0), indices(NULL), indicesSize(0)
{
}

VertexArray::VertexArray(unsigned verticesSize, unsigned indicesSize)
: vertices(NULL), verticesSize(0), indices(NULL), indicesSize(0)
{
    setSize(verticesSize, indicesSize);
}

VertexArray::VertexArray(const VertexArray& orig)
: vertices(NULL), verticesSize(0), indices(NULL), indicesSize(0)
{
    setSize(orig.verticesSize, orig.indicesSize);
    if (verticesSize > 0)
        memcpy(vertices, orig.vertices, verticesSize * sizeof(PackedVecColor));
    if (indicesSize > 0)
        memcpy(indices, orig.indices, indicesSize * sizeof(GLuint));
}

VertexArray::~VertexArray()
{
    setSize(0, 0);
}

VertexArray& VertexArray::operator=(const VertexArray& orig)
{
    setSize(orig.verticesSize, orig.indicesSize);
    if (verticesSize > 0)
        memcpy(vertices, orig.vertices, verticesSize * sizeof(PackedVecColor));
    if (indicesSize > 0)
        memcpy(indices, orig.indices, indicesSize * sizeof(GLuint));
    return *this;
}

void VertexArray::setSize(unsigned verticesSize, unsigned indicesSize)
{
    if (verticesSize != this->verticesSize) {
        if (this->verticesSize > 0) {
            delete vertices;
            this->verticesSize = 0;
        }
        if (verticesSize > 0) {
            vertices = new PackedVecColor[verticesSize];
            this->verticesSize = verticesSize;
        }
    }
    
    if (indicesSize != this->indicesSize) {
        if (this->indicesSize > 0) {
            delete [] indices;
            this->indicesSize = 0;
        }
        if (indicesSize > 0) {
            indices = new GLuint[indicesSize];
            this->indicesSize = indicesSize;
        }
    }
}
